package com.niit.musicserver.handler;

import com.niit.musicserver.common.Result;
import com.niit.musicserver.exception.MusicException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Description: 全局异常处理类
 */
@Slf4j
@ControllerAdvice(annotations={RestController.class})
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){
        log.error("【GlobalExceptionHandler.handlerException】系统异常，原因： {}", e.getMessage());
        e.printStackTrace();
        return Result.faile("系统异常，请稍后再试！");
    }
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public Result handlerArithmeticException(ArithmeticException e){
        log.error("【GlobalExceptionHandler.handlerArithmeticException】系统异常,原因：{}", e.getMessage());
        e.printStackTrace();
        return Result.faile("系统异常，算术错误！");
    }
    @ResponseBody
    @ExceptionHandler(MusicException.class)
    public Result handlerMusicException(MusicException e){
        log.error("【GlobalExceptionHandler.handlerMusicException】系统异常，原因：{}", e.getMessage());
        e.printStackTrace();
        return Result.faile(Integer.valueOf(e.getCode()), e.getMessage());
    }
}